/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-1-16         admin
 */

package com.coocaa.launcher.sidebar;

import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class ReadXMLParser {

    public static ParseredSetting parserXML(InputStream is) throws Exception {
        ParseredSetting data = new ParseredSetting();
        int type = 0;
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "UTF-8");
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("leftside")) {
                        data.setLeftSettings(new ArrayList<SettingData>());
                        type = 0;
                    } else if (parser.getName().equals("desksetting")) {
                        data.setDeskSettings(new ArrayList<SettingData>());
                        type = 1;
                    } else if (parser.getName().equals("systemsetting")) {
                        data.setSysSettings(new ArrayList<SettingData>());
                        type = 2;
                    } else if (parser.getName().equals("item")) {
                        SettingData settingData = new SettingData();
                        settingData.iconId = parser.getAttributeValue(null, "icon");
                        settingData.type = parser.getAttributeValue(null, "type");
                        settingData.titleId = parser.getAttributeValue(null, "titleid");
                        settingData.uri = parser.getAttributeValue(null, "uri");
                        switch (type) {
                            case 0:
                                data.getLeftSettings().add(settingData);
                                break;
                            case 1:
                                data.getDeskSettings().add(settingData);
                                break;
                            case 2:
                                data.getSysSettings().add(settingData);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    break;
            }
            eventType = parser.next();
        }
        is.close();
        return data;
    }

}
